################################################################################
#
# CMake script for building the simple compressor test
# Author: Milan Vukov, milan.vukov@esat.kuleuven.be
#
################################################################################

CMAKE_MINIMUM_REQUIRED( VERSION 2.8 )

################################################################################
#
# Project settings
#
################################################################################

#
# Minimum required version of cmake 
#
CMAKE_MINIMUM_REQUIRED( VERSION 2.8 )

#
# Project name and programming languages used
#
PROJECT( SIMPLE_COMPRESSOR CXX C )

#
# Folder path for generated executables
#
SET( EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR} )

#
# Folder path for generated libraries
#
SET( LIBRARY_OUTPUT_PATH ${PROJECT_BINARY_DIR}/lib )

#
# CMake module(s) path
#
SET( CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${PROJECT_SOURCE_DIR} ${PROJECT_SOURCE_DIR}/.. )

################################################################################
#
# Prerequisites
#
################################################################################

FIND_PACKAGE( ACADO REQUIRED )
FIND_PACKAGE( OCG2 REQUIRED )
FIND_PACKAGE( qpDUNES REQUIRED )

################################################################################
#
# User configuration
#
################################################################################

IF( NOT CMAKE_VERBOSE_MAKEFILE )
	SET( CMAKE_VERBOSE_MAKEFILE OFF )
ENDIF( NOT CMAKE_VERBOSE_MAKEFILE )

#
# Build type
#
IF( NOT CMAKE_BUILD_TYPE )
	SET(CMAKE_BUILD_TYPE Release CACHE STRING
		"Choose the type of build, options are: None Debug Release RelWithDebInfo MinSizeRel."
		FORCE
	)
ENDIF( NOT CMAKE_BUILD_TYPE )

IF( NOT QPSOLVER_TYPE )
	SET(QPSOLVER_TYPE qpOASES CACHE STRING
		"Choose the QP solver, options are: qpOASES, FORCES, qpDUNES."
		FORCE
	)
ENDIF( NOT QPSOLVER_TYPE )

IF (${QPSOLVER_TYPE} STREQUAL "qpDUNES" AND NOT ${qpDUNES_FOUND})
	MESSAGE( ERROR "Cannot proceed without qpDUNES source files." )
ENDIF()

MESSAGE( STATUS "Using QP solver: ${QPSOLVER_TYPE}" )

################################################################################
#
# Compiler settings
#
################################################################################

ADD_DEFINITIONS( -DACADO_CMAKE_BUILD )

#
# C compiler flags
#
SET( CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -pedantic -Wfloat-equal -Wshadow -O3" )
SET( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -pedantic -Wfloat-equal -Wshadow -O3" )

IF ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Intel")
	MESSAGE( STATUS "Using extra Intel compiler flags" )
	
	SET( CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -xHost -vec-report3 -opt-report3" )
	SET( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -xHost -vec-report3" )
ENDIF()

IF (${QPSOLVER_TYPE} STREQUAL "qpDUNES")
# 	SET( CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -U__MEASURE_TIMINGS__ -U__USE_ASSERTS__ -std=c99" )
	SET( CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -D__SUPPRESS_ALL_OUTPUT__ -U__MEASURE_TIMINGS__ -U__USE_ASSERTS__ -std=c99" )
ENDIF()

################################################################################
#
# Include directories
#
################################################################################

INCLUDE_DIRECTORIES(
    ${CMAKE_CURRENT_SOURCE_DIR}
    ${ACADO_INCLUDE_DIRS}
    ${OCG2_INCLUDE_DIRS}
    ${qpDUNES_INCLUDE_DIRS}
    ${PROJECT_BINARY_DIR}/simple_compressor_export
)

IF (${QPSOLVER_TYPE} STREQUAL "qpOASES")

	MESSAGE( STATUS "Added: ${QPSOLVER_TYPE}" )

	INCLUDE_DIRECTORIES(
		${PROJECT_BINARY_DIR}/simple_compressor_export/qpoases

	    ${ACADO_QPOASES_EMBEDDED_INC_DIRS}
	)
ELSEIF (${QPSOLVER_TYPE} STREQUAL "FORCES")

	MESSAGE( STATUS "Added: ${QPSOLVER_TYPE}" )

	INCLUDE_DIRECTORIES(
		${PROJECT_BINARY_DIR}/forces/include
	)
ELSE (${QPSOLVER_TYPE} STREQUAL "qpDUNES")
	MESSAGE( STATUS "Added: ${QPSOLVER_TYPE}" )

	INCLUDE_DIRECTORIES(
		${qpDUNES_INCLUDE_DIRS}
	)
ELSE()
	MESSAGE( ERROR "ooops..." )
ENDIF ( )

################################################################################
#
# Build applications, libraries and examples
#
################################################################################

#
# A list of autogenerated files
#
SET( GENERATED_FILES
	simple_compressor_export/acado_common.h
	simple_compressor_export/acado_integrator.c
	simple_compressor_export/acado_solver.c
	simple_compressor_export/acado_auxiliary_functions.c
)

IF (${QPSOLVER_TYPE} STREQUAL "qpOASES")
	SET( GENERATED_FILES
		${GENERATED_FILES}

		simple_compressor_export/qpoases/solver.cpp
		simple_compressor_export/qpoases/solver.hpp
	)
	
	SET( SOLVER_FILES
		${GENERATED_FILES}
		
		# qpOASES embedded sources
		${ACADO_QPOASES_EMBEDDED_SOURCES}
	)
	
ELSEIF (${QPSOLVER_TYPE} STREQUAL "FORCES")
	SET( SOLVER_FILES
		${GENERATED_FILES}
		
		${PROJECT_BINARY_DIR}/simple_compressor_export/forces/src/forces.c
	)
	
ELSEIF (${QPSOLVER_TYPE} STREQUAL "qpDUNES")
	SET( SOLVER_FILES
		${GENERATED_FILES}
	
		${qpDUNES_SOURCES}
	)
	
ENDIF( )

#
# Make a test file
#
ADD_EXECUTABLE( simple_compressor_test
	${PROJECT_SOURCE_DIR}/../simple_compressor_test.cpp
	
	${SOLVER_FILES}
)
IF( ${CMAKE_SYSTEM_NAME} MATCHES "Linux" )
	TARGET_LINK_LIBRARIES(
		simple_compressor_test
		rt
	)
ENDIF( )
